package com.example.demo8.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo8.config.MqttPushClient;
import com.example.demo8.data.IotDeviceSet;
import com.example.demo8.data.IotDeviceStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MsgController {

    @Autowired
    private MqttPushClient mqttPushClient;

    @PostMapping(value = "/cmd")
    public Object cmd (@RequestBody IotDeviceStatus iotDeviceStatus) {
        String content = JSON.toJSONString(iotDeviceStatus);
        boolean isSuccess = mqttPushClient.publish(1, false, "cmd/" + iotDeviceStatus.getDeviceNum(), content);
        return isSuccess;
    }

    @PostMapping(value = "/updateStatus")
    public Object updateStatus (@RequestBody IotDeviceStatus iotDeviceStatus) {
        String content = JSON.toJSONString(iotDeviceStatus);
        boolean isSuccess = mqttPushClient.publish(1, false, "status/set/" + iotDeviceStatus.getDeviceNum(), content);
        return isSuccess;
    }

    @GetMapping(value = "/getStatus/{deviceNum}")
    public Object getStatus (@PathVariable("deviceNum") String deviceNum) {
        boolean isSuccess = mqttPushClient.publish(0, false, "status/get/" + deviceNum, "wumei.live");
        return isSuccess;
    }

    @PostMapping(value = "/updateSetting")
    public Object updateSetting (@RequestBody IotDeviceSet iotDeviceSet) {
        String content = JSON.toJSONString(iotDeviceSet);
        boolean isSuccess = mqttPushClient.publish(0, false, "setting/set/" + iotDeviceSet.getDeviceNum(), content);
        return isSuccess;
    }

    @GetMapping(value = "/getSetting/{deviceNum}")
    public Object getSetting (@PathVariable("deviceNum") String deviceNum) {
        boolean isSuccess = mqttPushClient.publish(0, false, "setting/get/" + deviceNum, "wumei.lie");
        return isSuccess;
    }
}
